<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .txt {
            /* border: 1px solid #f00; */
            position: absolute;
            left: 150px;
            top: 50px;
            z-index: -1;
        }
        
        #canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="400" height="100"></canvas>
    <div class="txt">傻宝宝</div>
</body>
<script>
    let cvsDOM = document.querySelector('#canvas')
    let context = cvsDOM.getContext('2d')

    context.fillStyle = 'darkgray';
    context.fillRect(0, 0, 400, 100);

    context.fillStyle = '#fff';
    context.font = '30px sans-serif';
    context.fillText('刮刮乐', 160, 60);

    //控制是否按下后进行刮卡动作
    let isDraw = false
        //拖拽,鼠标按下，如果是移动端，touchstart
    cvsDOM.onmousedown = function() {
        console.log('按下')
        isDraw = true
    }

    //鼠标按下,如果是移动端，touchmove
    cvsDOM.onmousemove = function(e) {
        if (!isDraw) return
        console.log('移动')

        //核心，先获取鼠标移动x,y值，然后拿x,y值当作我要圆圈移动位置
        let x = e.pageX - this.offsetLeft
        let y = e.pageY - this.offsetTop

         context.globalCompositeOperation = 'destination-out';

        context.beginPath();

        //拿arc圆圈当橡皮来擦除
        context.arc(x, y, 5, 0, Math.PI / 180 * 360, false);
        context.fill();
        context.closePath();


    }

    //鼠标抬起,如果是移动端，touchend
    cvsDOM.onmouseup = function() {
        console.log('抬起')

        isDraw = false
    }
</script>

</html>